<template>
	<view class="dishes-detail">
		<u-navbar :title="dishName" @leftClick="leftClick" :autoBack="true"></u-navbar>
		<u--image
			class="dish-img"
			width="100vw"
			height="700rpx"
			mode="aspectFill"
			:src="dishImgUrl"
		>
			<template v-slot:loading>
				<u-loading-icon color="red"></u-loading-icon>
			</template></u--image>
		<view class="dish-container">
			<view class="dish-title">材料</view>
			<view class="common-box ingredients-box">
				<view v-for="(item,index) in detailInfo.ingredients" :key="index" class="cb-item">
					<view class="cb-i-name">{{ item.name }}</view>
					<view class="cb-i-num">{{ item.num }}</view>
				</view>
				<view v-if="(detailInfo.ingredients || []).length === 0" class="common-box-null">暂未录入~</view>
			</view>
			<view class="dish-title">步骤</view>
			<view class="common-box ingredients-box">
				<view v-for="(item,index) in detailInfo.steps" :key="index" class="cb-item">
					<view class="cb-i-name">{{ index + 1 }}、{{ item.step }}</view>
				</view>
				<view v-if="(detailInfo.steps || []).length === 0" class="ingredients-box-null">暂未录入~</view>
			</view>
			<view class="dish-title">备注</view>
			<view class="common-box remark-box">
				{{ detailInfo.remarks || '暂未录入~' }}
			</view>
		</view>
		<view class="edit-btn-box">
			<u-button
				type="primary"
				text="编 辑"
				color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))"
				@click="editDish"
			></u-button>
		</view>
	</view>
</template>

<script>
	import AuthorizationToken from '@/config/token.js'
	import baseUrl from '@/config/index.js'
	export default {
		data () {
			return {
				name: 'detail',
				detailInfo: {},
				baseUrl: 'https://lyw.cdbytech.top:3017',
				pkid: '',
				dishName: '菜品详情'
			}
		},
		computed: {
			dishImgUrl() {
				if (Array.isArray(this.detailInfo.cover)) {
					const info = this.detailInfo.cover[0] || {}
					return this.baseUrl + info.url
				}
				return this.baseUrl + this.detailInfo.cover?.url || ''
			}
		},
		onLoad(options) {
			this.pkid = options.pkid || ''
			console.log('_pkid', this.pkid)
			this.getDetailInfo()
		},
		methods: {
			leftClick() {
				uni.navigateTo({
					url: '/pages/index/index'
				})
			},
			getDetailInfo() {
				const vm = this
				uni.request({
					header: {
						Authorization: AuthorizationToken
					},
					method: 'POST',
					url: baseUrl + '/agw/mongodb/table/data_v3',
					data: {
						pkid: vm.pkid,
						tablePkid: '4163d0000b7a4b2990392d5a534b7271'
					},
					success(res) {
						console.log('返回结果', res)
						vm.detailInfo = res.data?.data || {}
						vm.dishName = vm.detailInfo.name 
						console.log('详情', vm.detailInfo)
					}
				})
			},
			editDish() {
				uni.navigateTo({
					url: '/pages/index/add?pkid=' + this.detailInfo.pkid
				})
			}
		}
	}
</script>

<style lang="scss">
	.dishes-detail{
		.dish-img{
			width: 100vh;
		}
		.dish-container{
			box-sizing: border-box;
			padding: 20rpx;
			.dish-title{
				font-size: 34rpx;
				font-weight: bold;
				letter-spacing: 2rpx;
				margin-bottom: 10rpx;
				color: #282F45;
			}
			.common-box{
				box-sizing: border-box;
				padding: 15rpx 30rpx;
				background-color: #fff;
				border-radius: 20rpx;
				margin-bottom: 30rpx;
				&-null{
					color: #6E737F;
					padding: 20rpx;
				}
			}
			.ingredients-box{
				.cb-item{
					display: flex;
					justify-content: space-between;
					padding: 15rpx 0;
					font-size: 30rpx;
					color: #6E737F;
					&:not(:last-child) {
						border-bottom: 1rpx solid #f1f2f3;
					}
				}
			}
			.remark-box{
				color: #6E737F;
				padding: 40rpx;
			}
			.ingredients-box-null{
				color: #6E737F;
				padding: 20rpx;
			}
		}
		.edit-btn-box{
			box-sizing: border-box;
			padding: 20rpx;
		}
	}
</style>